package com.wt.admin.config;

import com.wt.admin.config.cache.Cache;
import com.wt.admin.config.cache.CacheManager;
import com.wt.admin.domain.entity.sys.SysSettingEntity;
import com.wt.admin.domain.vo.sys.UserVO;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class GlobalBeanConfig {

    /**
     * 国际化
     * @return
     */
    @Bean("messageSource")
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:i18n/msg");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    /**
     * 用户缓存
     * @return
     */
    @Bean
    public Cache<Long, UserVO> userCache(){
        return CacheManager.getCache("user");
    }

    /**
     * 缓存配置
     * @return
     */
    @Bean
    public Cache<String, SysSettingEntity> settingCache(){
        return CacheManager.getCache("setting");
    }

    /**
     * 异步线程
     * @return
     */
    @Bean("newAsyncExecutor")
    public Executor newAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(50);
        taskExecutor.setMaxPoolSize(100);
        taskExecutor.setQueueCapacity(50);
        taskExecutor.setKeepAliveSeconds(100);
        taskExecutor.setThreadNamePrefix("定时任务-");
        taskExecutor.initialize();
        return taskExecutor;
    }

    /**
     * 通用线程池
     * @return
     */
    @Bean("publicThread")
    public Executor publicThread() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(50);
        taskExecutor.setMaxPoolSize(100);
        taskExecutor.setQueueCapacity(50);
        taskExecutor.setKeepAliveSeconds(100);
        taskExecutor.setThreadNamePrefix("通用任务-");
        taskExecutor.initialize();
        return taskExecutor;
    }




}
